package com.nsu.cl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nsu.cl.pojo.Subject;
import com.nsu.cl.service.SubjectService;
import com.nsu.cl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "科目控制器")
@RestController
@RequestMapping("/subjectController")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @ApiOperation("获取全部科目")
    @GetMapping("/getSubjects")
    public Result getSubjects() {
        List<Subject> subjects = subjectService.getSubjects();
        return Result.ok(subjects);
    }

    @ApiOperation("获取科目ID")
    @GetMapping("/getSubjectByName")
    public Result getSubjectByName(@ApiParam("查询条件") Subject subject) {
        Subject sub = subjectService.getByName(subject);
        return Result.ok(sub);
    }

    @ApiOperation("获取详情科目")
    @GetMapping("/getSubjectById/{id}")
    public Result getSubjectById(@ApiParam("科目id")@PathVariable("id") Long id) {
        return Result.ok(subjectService.getById(id));
    }

    @ApiOperation("删除Subject信息")
    @DeleteMapping("/deleteSubject")
    public Result deleteSubject(@ApiParam("要删除的所有的subject的id的JSON集合") @RequestBody List<Integer> ids) {
        // 调用服务层
        subjectService.removeByIds(ids);

        return Result.ok();
    }

    @ApiOperation("新增或修改Subject，有id属性则是修改，没有则是增加")
    @PostMapping("/saveOrUpdateSubject")
    public Result saveOrUpdateSubject(@ApiParam("JSON的Subject对象")@RequestBody Subject subject) {
        // 接收参数
        // 调用服务层方法完成增加或者修改
        subjectService.saveOrUpdate(subject);
        return Result.ok();
    }

    @ApiOperation("查询根据科目名称模糊查询，带分页")
    @GetMapping("/getSubjects/{pageNo}/{pageSize}")
    public Result getSubjects(@ApiParam("分页查询页码数")@PathVariable("pageNo") Integer pageNo,
                            @ApiParam("分页查询页大小")@PathVariable("pageSize")Integer pageSize,
                            @ApiParam("分页查询模糊查询的名称")String name) {
        // 分页 带条件查询
        Page<Subject> page = new Page<>(pageNo,pageSize);
        // 通过服务层
        IPage<Subject> pageRs = subjectService.getSubjectByOpr(page,name);

        // 封装Result对象并返回
        return Result.ok(page);
    }
}
